<!--
 * @Author: yishen@mdashen.com
 * @Date: 2021-05-19 20:21:55
 * @LastEditTime: 2021-05-29 16:24:21
 * @Description: 下拉菜单组件
-->
<template>
  <div class="dropdown" ref="dropdownRef">
    <a
      class="btn btn-outline-light my-2 dropdown-toggle"
      href="#"
      role="button"
      id="dropdownMenuLink"
      data-bs-toggle="dropdown"
      aria-expanded="false"
      @click.prevent="toggleOpen"
    >
      {{ title }}
    </a>
    <ul
      class="dropdown-menu"
      :style="{ display: 'block' }"
      aria-labelledby="dropdownMenuButton1"
      v-if="isOpen"
    >
      <slot></slot>
    </ul>
  </div>
</template>

<script lang="ts">
import useClickOutside from '../hooks/useClickOutside'
import { defineComponent, watch, ref } from 'vue'
export default defineComponent({
  name: 'Dropdown',
  components: {},
  props: {
    title: {
      type: String,
      required: true
    }
  },
  setup () {
    const isOpen = ref(false)
    const dropdownRef = ref<null | HTMLElement>(null)
    const toggleOpen = () => {
      isOpen.value = !isOpen.value
    }
    const isClickOUtside = useClickOutside(dropdownRef)
    watch(isClickOUtside, () => {
      if (isOpen.value === true && isClickOUtside.value === true) {
        isOpen.value = false
      }
    })
    return {
      toggleOpen,
      isOpen,
      dropdownRef
    }
  }
})
</script>
<style lang="scss" scoped>
.dropdown {
}
</style>
